package com.block.datapush.special.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.common.exception.BizException;
import com.block.common.exception.NotifyExceptionService;
import com.block.common.service.LogFactory;
import com.block.datapush.form.enums.PushActionTypeEnum;
import com.block.datapush.special.service.ISpecialCostomInfoProfitService;
import com.block.db.entity.SpecialCostomInfo;
import com.block.db.entity.SpecialCostomInfoProfit;
import com.block.db.mapper.SpecialCostomInfoMapper;
import com.block.db.mapper.SpecialCostomInfoProfitMapper;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;

/**
 * <p>
 * 机构收益表 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-03-13
 */
@Service
public class SpecialCostomInfoProfitServiceImpl extends ServiceImpl<SpecialCostomInfoProfitMapper, SpecialCostomInfoProfit> implements ISpecialCostomInfoProfitService {

    private static Logger log = LogFactory.getPushLog();

    @Resource
    SpecialCostomInfoMapper specialCostomInfoMapper;
    @Autowired
    NotifyExceptionService notifyExceptionService;

    /**
     * 保存机构推送的收益信息
     */
    @Async
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public void saveSpecialPushProfitInfo(String infoId, BigDecimal priceAmount, PushActionTypeEnum actionTypeEnum){

        try {
            String nowDate = LocalDate.now().toString();
            SpecialCostomInfoProfit infoProfit = this.baseMapper.getEntityByDateInfoAndInfoId(infoId, nowDate);
            if (ObjectUtil.isEmpty(infoProfit)){

                SpecialCostomInfo costomInfo = specialCostomInfoMapper.selectById(infoId);

                infoProfit = new SpecialCostomInfoProfit();
                infoProfit.setInfoId(String.valueOf(costomInfo.getId()));
                infoProfit.setInfoName(costomInfo.getName());
                infoProfit.setDateInfo(nowDate);

                boolean save = this.save(infoProfit);
                log.info("【新增机构推送收益信息】【save】{}【infoId】{}【nowDate】{}", save, infoId, nowDate);
                if (!save){
                    String  msg = StrUtil.format("新增记录机构推送收益异常【infoId】{}【dateInfo】{}【priceAmount】{}【actionTypeEnum】{}",
                            infoId, nowDate, priceAmount, actionTypeEnum);
                    throw new BizException(msg);
                }

                infoProfit  = this.getById(infoProfit.getId());
            }

            SpecialCostomInfoProfit updateEntity = new SpecialCostomInfoProfit();
            updateEntity.setId(infoProfit.getId());

            if (StrUtil.equals(actionTypeEnum.getType(), PushActionTypeEnum.CHECK.getType())){
                // 撞库失败
                updateEntity.setMatchNumber(infoProfit.getMatchNumber() + 1);
            }else if (StrUtil.equals(actionTypeEnum.getType(), PushActionTypeEnum.REGISTER.getType())){
                // 推送失败
                updateEntity.setMatchNumber(infoProfit.getMatchNumber() + 1);
                updateEntity.setRegisterNumber(infoProfit.getRegisterNumber() + 1);
            }else if (StrUtil.equals(actionTypeEnum.getType(), PushActionTypeEnum.SUCCESS.getType())) {
                // 推送成功
                updateEntity.setMatchNumber(infoProfit.getMatchNumber() + 1);
                updateEntity.setRegisterNumber(infoProfit.getRegisterNumber() + 1);
                updateEntity.setPushNumber(infoProfit.getPushNumber() + 1);
                updateEntity.setProfitAmount(infoProfit.getProfitAmount().add(priceAmount));
            }
            // 根据操作类型，进行设置不同的更新数据
            boolean update = this.updateById(updateEntity);
            log.info("【更新机构推送收益信息】【update】{}【infoId】{}【nowDate】{}【actionType】{}",
                    update, infoId, nowDate, actionTypeEnum);
            if (!update){
                String  msg = StrUtil.format("更新机构推送收益异常【infoId】{}【dateInfo】{}【priceAmount】{}【actionTypeEnum】{}",
                        infoId, nowDate, priceAmount, actionTypeEnum);
                throw new BizException(msg);
            }

        }catch (Exception exception){
            String msg = StrUtil.format("【更新机构推送收益信息】更新异常【infoId】{}【actionType】{}",infoId,  actionTypeEnum);
            log.error(msg);
            notifyExceptionService.notifyNoticeInfo("机构推送收益", "更新异常", msg, exception);
        }

    }

}
